/**
 * Carnegie Mellon University
 * School of Computer Science
 * MSIT eBusiness Technology
 * 
 * Team 8 - Infinity (2011 Spring)
 * infinity-team8@googlegroups.com
 * 
 * Professor: Jeffrey L. Eppinger
 * Program Faculty: Sujata Telang
 */

package formbean;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.mybeans.form.FormBean;

public class EmployeeTransitionDayForm extends FormBean {
	private String	date;
	

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date.trim();
	}

	public List<String> getValidationErrors() {
		List<String> errors = new ArrayList<String>();

		if (date == null || date.toString().length() == 0) {
			errors.add("Date is required");
		}
		
		if (errors.size() > 0) {
			return errors;
		}
		
		if (date.matches(".*[<>\"].*")) {
			errors.add("Date may not contain angle brackets or quotes");
		}
		
		if (errors.size() > 0) {
			return errors;
		}
		
		
		if (date.length()!=10) {
			errors.add("Date must be in yyyy-mm-dd format");
		} else if (!date.substring(4,5).equals("-") || !date.substring(7,8).equals("-")) {
			errors.add("Date must be in yyyy-mm-dd format");
		}
		
		if (errors.size() > 0) {
			return errors;
		}
		
		String yyyy, mm, dd;
		yyyy = date.substring(0,4);
		mm = date.substring(5,7);
		dd = date.substring(8,10);
		
		try {
			Integer.parseInt(yyyy);
			Integer.parseInt(mm);
			Integer.parseInt(dd);
		} catch (NumberFormatException e) {
			errors.add("Date must be in yyyy-mm-dd format");
			errors.add("year/month/date must be numeric values");
		}
		
//		//validate YYYY-MM-DD format
//		try {
//			DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); // YYYY-MM-DD
//			df.setLenient(false);
//			df.parse(date);
//	    } catch (ParseException e) {
//	    	errors.add("Date must be in yyyy-mm-dd format");
//	    } catch (IllegalArgumentException e) {
//	    	errors.add("Date must be in yyyy-mm-dd format");
//	    }
		
	    if (errors.size() > 0) {
			return errors;
		}
	    
	    
	    //using Gregorian Calendar to check for user input errors
	    try {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setLenient(false);
            gc.set(GregorianCalendar.YEAR, Integer.parseInt(yyyy));// possible invalid year
            gc.set(GregorianCalendar.MONTH, Integer.parseInt(mm)-1);// possible invalid month
            gc.set(GregorianCalendar.DATE, Integer.parseInt(dd));// possible invalid date

            gc.getTime(); // exception thrown here
        }
        catch (Exception e) {
        	errors.add("year/month/date values need to be in the correct ranges accordingly");
        }

		return errors;
	}
}
